$(function(){
	// 点击选中状态切换
//	$('.fa').parent().click(function(){
//		var flag;
//		if ($(this).find('.fa').hasClass('fa-square-o')) {// 选中
//			flag = true;
//		} else {
//			flag = false;
//		}
//		checkFaSquare($(this), flag);
//	});
	
	
	$('#cancel').click(function(){
		var tabName = '';
		if (publishId != '') {
			tabName = '发布申请编辑';
		} else {
			tabName = '发布资讯';
		}
		
		backendCommon.closeInTabs(tabName);
	});
	
	$('.sort-input').keyup(function(){
		var num = $.trim($(this).val());
		if (num != '-' && isNaN(num)) {
			$(this).val('');
		}
	});
	
	$('#sort-div').delegate('.del-row', 'click', function(){
		var delRow = $('.del-row');
		if (delRow.length > 1) {
			$(this).parent('.sort-row').remove();
		} else {
			// 暂停
		}
	});
	
	$('#sub-immediately').click(function(){
		$('#submitTime').val('');
	});
	
	$('#submitTime').blur(function(){
		var submit = $('input[name=subType]:checked');
		if (submit.length == 0 || submit.val() == 1) {
			$(this).val('');
		}
	});
	
	function preventSelectDisabled(oSelect) {
	   //得到当前select选中项的disabled属性。
	   var isOptionDisabled = oSelect.options[oSelect.selectedIndex].disabled;
	   //如果是有disabled属性的话
	   if(isOptionDisabled) {
	      //让他恢复上一次选择的状态，oSelect.defaultSelectedIndex属性是前一次选中的选项index
	      oSelect.selectedIndex = oSelect.defaultSelectedIndex;
	      return false;
	   } else {//如果没有disabled属性的话
	       //oSelect.defaultSelectedIndex属性，设置成当前选择的index
	       oSelect.defaultSelectedIndex = oSelect.selectedIndex;
	       return true;
	   }
	}
	
	$("select").each(function(){
	    this.onchange=function(){
	    	if(!preventSelectDisabled(this)){return false;};
	      };
	    }
	 );
	
	//选中checkbox
	function checkFaSquare(_this, flag){
		var fa = _this.find('.fa');
		if (flag) {
			fa.addClass('fa-check-square-o').removeClass('fa-square-o');
		} else {
			fa.addClass('fa-square-o').removeClass('fa-check-square-o');
		}
	}
	
	// 位置处checkbox选中
	$('div.row a').click(function(){
		var flag = $(this).find('.fa').hasClass('fa-square-o');
		checkFaSquare($(this), flag);
	});
	
	// 发布对象所有版本切换
	$('#pub-all').click(function(e, data){
		var fa = $(this).find('.fa'), flag;
		if (typeof data != 'undefined') {
			flag = data;
		} else {
			flag = fa.hasClass('fa-square-o');
		}
		
		checkFaSquare($(this), flag);
		checkFaSquare($('#pub-turist'), flag);
		
		$('#pub-obj .col_head').each(function(){
			$(this).trigger('click', flag);
		});
	});
	
	// 学生 老师 竖排选择
	$('#pub-obj .col_head').click(function(e, data){
		var elem, flag ;
		if (typeof data != 'undefined') {
			flag = data;
		} else {
			flag = $(this).find('.fa').hasClass('fa-square-o');
		}
		checkFaSquare($(this), flag);
		if ($(this).hasClass('col_1')) {// 第一列
			checkFaSquare(elem = $('#pub-obj .col_1:not(.col_head)'), flag);
		} else if($(this).hasClass('col_2')) {
			checkFaSquare(elem = $('#pub-obj .col_2:not(.col_head)'), flag);
		}
		
		if (elem) {
			// 学校类型是否选中判断
			$.each(elem, function(index, value){
				var chckd = $(this).parents('.fn-clear').find('.fn-left:not(.fist-row) .fa-check-square-o');
				var head = $(this).parent('.fn-left').siblings('.fist-row').find('a');
				if (chckd) {
					if (chckd.length == 1) {
						checkFaSquare(head, true);
					} else {
						checkFaSquare(head, flag);
					}
				}
			});
		}
		
		if (typeof data == 'undefined') {
			if (!flag) {// 个性化竖排取消选中，取消所有版本
				if ($('#pub-all .fa').hasClass('fa-check-square-o')) {
					checkFaSquare($('#pub-all'), false);
				}
			} else {// 个性竖排选中，判断是否全部选中，是选中所有版本
				if ($('#pub-turist').find('.fa').hasClass('fa-check-square-o')) {
					// 游客版已选中
					if ($('.fist-col .fa.fa-check-square-o').length == 2) {// 个性竖排已选中
						checkFaSquare($('#pub-all'), true);
					}
				}
			}
		}
	});
	
	// 游客版点击
	$('#pub-turist').click(function(){
		var flag = $(this).find('.fa').hasClass('fa-square-o');
		checkFaSquare($(this), flag);
		if (flag) {
			var count = $('#primary,#middle,#high').find('.fa-check-square-o').length;
			if (count == 9) {
				// 个性所有已选中
				checkFaSquare($('#pub-all'), flag);
			}
		} else {
			checkFaSquare($('#pub-all'), flag);
		}
	});
	
	//个性横排点击
	$('#pub-obj .row_head').click(function(){
		var elem, flag = $(this).find('.fa').hasClass('fa-square-o');
		checkFaSquare($(this), flag);
		
		if ($(this).hasClass('row_1')) {// 第一列
			elem = $('#pub-obj .row_1:not(.row_head)');
		} else if($(this).hasClass('row_2')) {
			elem = $('#pub-obj .row_2:not(.row_head)');
		} else if ($(this).hasClass('row_3')) {
			elem = $('#pub-obj .row_3:not(.row_head)');
		}
		
		checkFaSquare(elem, flag);
		if (elem) {
			$.each(elem, function(index, value){
				var col;
				if ($(this).hasClass('col_1')) {
					col = 'col_1';
				} else if ($(this).hasClass('col_2')) {
					col = 'col_2';
				}
				
				var chck = $('#pub-obj .'+col+':not(.col_head) .fa-check-square-o');
				if (chck.length == 0) {
					checkFaSquare($('#pub-obj .'+col+'.col_head'), false);
				} else {
					checkFaSquare($('#pub-obj .'+col+'.col_head'), true);
				}
			});
		}
		
		if (flag) {//横排选中
			if ($('#pub-turist').find('.fa').hasClass('fa-check-square-o')) {
				var count = $('#primary,#middle,#high').find('.fa-check-square-o').length;
				if (count == 9) {
					checkFaSquare($('#pub-all'), true);
				}
			}
		} else {
			if ($('#pub-all .fa').hasClass('fa-check-square-o')) {
				checkFaSquare($('#pub-all'), false);
			}
		}
	});
	
	// 个性化内部点击
	$('#pub-obj .col_1:not(.col_head),#pub-obj .col_2:not(.col_head)').click(function(){
		var flag = $(this).find('.fa').hasClass('fa-square-o');
		checkFaSquare($(this), flag);
		
		var col;
		if ($(this).hasClass('col_1')) {
			col = 'col_1';
		} else if ($(this).hasClass('col_2')) {
			col = 'col_2';
		}
		var row;
		if ($(this).hasClass('row_1')) {
			row = 'row_1';
		} else if ($(this).hasClass('row_2')) {
			row = 'row_2';
		} else if ($(this).hasClass('row_3')) {
			row = 'row_3';
		}
		if (flag) {
			checkFaSquare($('.'+col+'.col_head'), true);
			checkFaSquare($('.'+row+'.row_head'), true);
			if ($('#pub-turist').find('.fa').hasClass('fa-check-square-o')) {
				var count = $('#primary,#middle,#high').find('.fa-check-square-o').length;
				if (count == 9) {
					checkFaSquare($('#pub-all'), true);
				}
			}
		} else {// 取消
			if ($('#pub-obj .'+col+':not(.col_head) .fa-check-square-o').length == 0) {
				checkFaSquare($('.'+col+'.col_head'), false);
			}
			
			if ($('#pub-obj .'+row+':not(.row_head) .fa-check-square-o').length == 0) {
				checkFaSquare($('.'+row+'.row_head'), false);
			}
			
			if ($('#pub-all .fa').hasClass('fa-check-square-o')) {
				checkFaSquare($('#pub-all'), false);
			}
		}
	});
	
	$('#publish').click(function(){
		var infoPublishApplication ={},
		infoPublishDetailList = [],
		infoPublishObjectList = [];
		
		//----发布对象----
		var checkedTurist = $('#pub-turist .fa-check-square-o');
		var checkedSelect = $('#pub-select a:not(.col_head,.row_head) .fa-check-square-o');
		
		if (checkedTurist.length == 0 && checkedSelect.length == 0) {
			//选择发布对象
			return;
		}
		
		if (checkedTurist.length > 0) {
			var obj = {};//游客版
			obj['objType'] = 0;
			infoPublishObjectList.push(obj);
		}
		
		if (checkedSelect.length > 0) {
			$.each(checkedSelect, function(){
				var obj = {};
				var data = $(this).attr('data').split('_');
				
				obj['objType'] = 1;
				obj['objPhaseType'] = data[0];
				obj['objRoleCode'] = data[1];
				infoPublishObjectList.push(obj);
			});
		}
		
		//----发布申请----
		var slt = $('#selectSection option:selected');
		if (slt && slt.val() != -1) {
			infoPublishApplication['locThemeId'] = slt.val();
			infoPublishApplication['locChannelId'] = slt.attr('data-parent');
		} else {
			// 请选择栏目
			
			return;
		}
		
		var loc = $('#loc-operation .fa-check-square-o');
		if (loc.length > 0) {
			var locStr='';
			$.each(loc, function(){
				locStr += $(this).attr('data-id')+',';
			});
			locStr = locStr.substr(0, locStr.length - 1);
			infoPublishApplication['locOpration'] = locStr;
		} else {
			// 请选择运营位
			return;
		}
		
		var submit = $('input[name=subType]:checked');
		if (submit.length > 0) {
			infoPublishApplication['publishType'] = submit.val();
			if (submit.val() == 2) {
				var time = $('#submitTime').val();
				if (time) {
					infoPublishApplication['appointmentTime'] = time;
				} else {
					// 请选择预约发布时间
					return;
				}
			}
		} else {
			// 选择发布类型
			return;
		}
		
		// 资讯排序发布
		var sorts = $('#sort-div .sort-row');
		if (sorts.length > 0) {
			var flag = false;
			$.each(sorts, function(){
				var obj = {};
				var sort = $(this).find('.sort-input').val();
				if (sort && Number(sort)) {
					obj['infoSort'] = sort;
				} else {
					flag = true;
					return flase;
				}
				
				obj['infoTitle'] = $(this).find('#infoTitle').text();
				obj['infoId'] = $(this).find('.del-row').attr('data-id');
				obj['infoUrl'] = $(this).find('img').attr('data');
				infoPublishDetailList.push(obj);
			});
			
			if (flag) {
				//请填写排序数值
				return;
			}
		} else {
			// 为选择资讯
			return;
		}
		
		var infoPublishDTO = {};
		if (publishId != '') {
			infoPublishApplication['id'] = publishId;
		}
		infoPublishDTO['infoPublishApplication']=infoPublishApplication;
		infoPublishDTO['infoPublishDetailList'] = infoPublishDetailList;
		infoPublishDTO['infoPublishObjectList'] = infoPublishObjectList;
		
		console.log(infoPublishDTO)
		if (publishId != '') {// 编辑
			$.post(mainDomain+'/info/publish/edit.action', 
					{'infoPublishDTO':JSON.stringify(infoPublishDTO)}
			,function(res){
				alert("发布成功");
				window.location.href = mainDomain + "/info/infoPublishVerify.action";
			});
		} else {// 新增
			$.post(mainDomain+'/info/publish/add.action', 
					{'infoPublishDTO':JSON.stringify(infoPublishDTO)}
			,function(res){
				alert("发布成功");
				window.location.href = mainDomain + "/info/infoPublishVerify.action";
			});
		}
		
	});
	
	if (publishId != '') {//编辑
		// 初始化
		$.post(mainDomain+'/info/infoPublish/'+publishId+'.action', {},function(res){
			console.log(res);
			var ipa = {};
			if (res && res.success) {
				ipa = res.data.infoPublishApplicationVO;
				if (ipa.objectVoList) {// 初始化发布对象
					$.each(ipa.objectVoList, function(i, v){
						if (v.objType == 0) {
							$('#pub-turist').trigger('click');
						} else if (v.objType == 1) {
							var row = v.objPhaseType, col = v.objRoleCode;
							col == 3 ? col=2 : col;
							if (row && col) {
								$('.row_'+row + '.col_'+col).trigger('click');
							}
						}
					});
				}
				
				// sectionInfoVo
				if (ipa.locThemeId) {// 栏目
					$('#selectSection').val(ipa.locThemeId);
				}
				
				if (ipa.locOpration) {// 运营位
					var opr = ipa.locOpration.split(',');
					$.each(opr, function(i,v){
						if (v == 1) {
							$('#loc-operation .pos-first').trigger('click');
						}
					});
				}
				
				if (ipa.detailVoList) {// 资讯列表
					var sortArr = [];
					$.each(ipa.detailVoList, function(index, item){
						sortArr.push('<div class="sort-row">');
						sortArr.push('<input type="text" class="row sort-input" value="'+item.infoSort+'">');
						sortArr.push('<span id="infoTitle" class="row">'+item.infoTitle+'</span>');
						sortArr.push('<img class="row" src="'+staticDomain+(item.infoUrl ? item.infoUrl : "")+'">');
						sortArr.push('<input data-id="15" type="button" value="删除" class="del-row row"></div>');
					});
					$('#sort-div').append(sortArr.join(''));
				}
				
				if (ipa.publishType) {
					if (ipa.publishType == 1) {
						$('#sub-immediately').click();
					} else {
						$('#sub-later').click();
						// appointmentTime
						
						if (ipa.appointmentTime) {
							
							var dateStr = backendCommon.dateTimeToLocalStringWithSeconds(ipa.appointmentTime);
							$('#submitTime').val(dateStr);
						}
					}
				}
			}
		});
	}
});